package com.yayan.util;

import java.beans.PropertyVetoException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 
 * @author 刘超  2017/7/12
 *
 */
public class DButil {
    private static Properties prop=null;
    private static String driver = null; 
    private	static String  dbUrl=null;
    private static String  dbUser=null;
    private static String dbPsw=null;
    private static int maxSize=40;
    private	static int minSize=2;
    private	static int initSize=10;	
    private	static int maxchache=80;
    private static String propfile = "db.properties";

    private static DataSource dataSource;
    private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
    static{
    	prop = new Properties();
    	InputStream in=null;
    	try {
    		in = DButil.class.getClassLoader().getResourceAsStream(propfile);
			prop.load(in);
			driver = prop.getProperty("driver");
			dbUrl = prop.getProperty("dbUrl");
			dbUser = prop.getProperty("dbUser");
			dbPsw = prop.getProperty("dbPsw");
			maxSize = Integer.valueOf(prop.getProperty("maxSize"));
			minSize = Integer.valueOf(prop.getProperty("minSize"));
			initSize = Integer.valueOf(prop.getProperty("initSize"));
			maxchache = Integer.valueOf(prop.getProperty("maxchache"));
			dataSource = initDataSource();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(in!=null){
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
    	}
    }
    
    private static ComboPooledDataSource  initDataSource(){
    	ComboPooledDataSource ds = new ComboPooledDataSource();
    	try {
    		ds.setDriverClass(driver);
    		ds.setJdbcUrl(dbUrl);
    		ds.setUser(dbUser);
    		ds.setPassword(dbPsw);
    		ds.setMaxPoolSize(maxSize);
    		ds.setMinPoolSize(minSize);
    		ds.setInitialPoolSize(initSize);
    		ds.setMaxStatements(maxchache);
    		System.out.println("driver--"+driver+"   dbUrl---"+dbUrl+" dbUser="+dbUser+" dbPsw="+dbPsw);
		} catch (PropertyVetoException e) {
			e.printStackTrace();
		}
    	return ds;
    } 
       
    public static DataSource getDataSource(){
		return dataSource;
	}
    
	public static Connection getConnection() throws SQLException {
		Connection con = tl.get();
		if (con == null) {
			con = dataSource.getConnection();
			tl.set(con);
			//System.out.println("connection");
		}		
		return con;
	}

	public static void startTransaction() throws SQLException {
		//System.out.println("startTransaction");
		getConnection().setAutoCommit(false);
	}

	public static void rollback() throws SQLException {
		//System.out.println("rollback");
		getConnection().rollback();
	}
	
    public static void close(Connection conn,PreparedStatement pst,ResultSet rs){
    	close(rs);
    	close(pst);
    	close(conn);
    }
    
    public static void close(Connection conn){
    	if(conn!=null){  
            try {
                conn.close();
                tl.remove();
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
        }  
    }
    
    public static void close(PreparedStatement pst){
    	if(pst!=null){  
            try {  
                pst.close();  
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
        }  
    }
    
    public static void close(ResultSet rs){
    	if(rs!=null){  
            try {  
                rs.close();  
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
        }  
    }
    
	public static void commitAndReleased() throws SQLException {
		getConnection().commit();
		getConnection().close();
		tl.remove();		
	}

}
